package user;

import hibernate.HibernateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import lecture.Lecture;
import lecture.LectureAdminManagedBean;
import org.hibernate.Session;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
/**
 *
 * @author Thorsten Ludwig
 */

@Entity(name="userClass")
@ManagedBean(name="UserBean")
@CustomScoped(value="#{window}")
public class User implements Serializable {
    /**
     * users first name
     */
    private String firstName;

    /**
     * users last name
     */
    private String lastName;

    /**
     * unique username
     */
    private String username;

    /**
     * users student id
     */
    private String studentId;
    
    /**
     * user mail adress
     */
    private String email;
    
    private String aspiredGrade;
    
    private String course;
    
    @OneToMany
    @LazyCollection(LazyCollectionOption.TRUE)    
    private List<ScoreGroup> scoreGroups;

    
    @ElementCollection(fetch= FetchType.EAGER)
    @Enumerated(EnumType.STRING)
    @Cascade(CascadeType.ALL)
    private Map<String,Role> rights;
    @Id
    @GeneratedValue
    private Long id;
    
    public User () {
        
    }

    /**
     *
     * @param firstName
     * @param lastName
     * @param username
     * @param matrikelnummer
     * @param pw
     * @param email
     */
    public User (String firstName, 
                 String lastName, 
                 String username, 
                 String matrikelnummer,
                 String email,
                 String aspiredDegree,
                 String course) {
        System.out.println("Erstelle User " + username);
        this.firstName = firstName;
        this.lastName = lastName;
        this.username = username;
        this.studentId = matrikelnummer;
        this.email = email;
        this.course = course;
        this.rights = new HashMap<String,Role>();
        this.scoreGroups = new ArrayList<ScoreGroup>();
        FacesContext c = FacesContext.getCurrentInstance();
        LectureAdminManagedBean l = c.getApplication().evaluateExpressionGet(c, "#{lectureAdmin}", LectureAdminManagedBean.class);
        for (Lecture lecture: l.getLectures()) {
            System.out.println("Setze Rechte von " + this.firstName + "in der Vorlesung " + lecture.getInfo().getName());
            
            this.assignRole(lecture.getInfo().getName(), Role.GUEST);
        }
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<String> getIsAdminOf() {
        List<String> list = new ArrayList<String>();
        for (String lecture: this.rights.keySet()) {
            if ((this.rights.get(lecture) == Role.ADMIN) ||
                    (this.rights.get(lecture) == Role.COADMIN)) {
                list.add(lecture);
            }
        }
                
        return list;
    }

    public Role getUserRole(String name) {
        System.out.println("Rights von " + name + "sind : " + this.rights + "ID " + this);
        Role role = this.rights.get(name);
        return role;
    }

    public void assignRole(String name, Role role) {
        System.out.println("Setzte Rolle von" + this.username + " in " + name + " auf " +role);
        Session session = HibernateUtil.getSession();
        session.beginTransaction();
        this.rights.put(name, role);
        session.merge(this);
        session.getTransaction().commit();
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getAspiredGrade() {
        return aspiredGrade;
    }

    public void setAspiredGrade(String aspiredGrade) {
        this.aspiredGrade = aspiredGrade;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public List<ScoreGroup> getScoreGroups() {
        return scoreGroups;
    }

    public void setScoreGroups(List<ScoreGroup> scoreGroups) {
        this.scoreGroups = scoreGroups;
    }
    
    public void removeScoreGroup(ScoreGroup sg) {
        this.scoreGroups.remove(sg);
    }
    
    public void createScoreGroup() {
        this.scoreGroups.add(new ScoreGroup());
    }

    public Map<String, Role> getRights() {
        return rights;
    }

    public void setRights(Map<String, Role> rights) {
        this.rights = rights;
    }
}
